home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c
- Subject: Re: Trying to store a triangular matrix
- Date: 18 Jan 1996 17:48:45 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan18124845@g7240065.bridge.bst.bls.com>
- References: <sksen.821949883@merle>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: sksen@merle.acns.nwu.edu's message of 18 Jan 96 07:24:43 GMT
-
- In article <sksen.821949883@merle> sksen@merle.acns.nwu.edu (Subhro Sen) writes:
-
- : I am trying to store a 2D lower triangular N x N matrix in
- : a 1D array...in other words i want A[i][j] to
- : map to some index m. What's the formula!?!?!
- : It's a lower triangular matrix, (A[i][j] = 0 for j > i)
- : i.e.:
-
- : 1 0 0 0
- : 2 1 0 0
- : 5 3 2 0
- : 9 3 4 1
-
- : at first i had:
-
- : m = i*N + j where N=one dimension of matrix
-
- try:
- m = (i*(i+1))/2 + j;
-
- Instead of having a algorithm to map to one dimensional array
- why not use a pointer to pointer like:
-
- int** a;
- int i, j;
-
- a = malloc(sizeof(*a));
- a[0] = malloc(sizeof(**a) * (N * (N + 1)) / 2);
-
- for (i = 1; i < N; i++)
- a[i] = a[i-1] + i;
-
- This has the advantage of using the reduced space but still gives means
- of accessing like:
-
- a[i][j] = 5;
-
- But j must be <= i.
-
- Regards
-
- -A.
- --
- | A.Champion |
-